<template>
  <a-spin :spinning="confirmLoading">
    <div>qiaoooo</div>
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form :form="form" slot="detail">
        <a-row>
          <!-- <a-col :span="8" >
            <a-form-item label="基础信息" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['info']" placeholder="请输入基础信息"></a-input>
            </a-form-item>
          </a-col> -->
          <a-col :span="8" >
            <a-form-item label="果园名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['name', validatorRules.name]" placeholder="请输入果园名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8" >
            <a-form-item label="果园地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['address', validatorRules.address]" placeholder="请输入果园地址"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8" >
            <a-form-item label="建园时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择建园时间" v-decorator="['createdTime', validatorRules.createdTime]" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="8" >
            <a-form-item label="种植面积（亩）" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['area', validatorRules.area]" placeholder="请输入种植面积（亩）" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="8" >
            <a-form-item label="果树数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['treeCount']" placeholder="请输入果树数量" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="8" >
            <a-form-item label="果树品种" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-multi-select-tag type="list_multi" v-decorator="['variety', validatorRules.variety]" :trigger-change="true" dictCode="txpp_tree_varieties,name,id" placeholder="请选择果树品种"/>
            </a-form-item>
          </a-col>
          <a-col :span="8" >
            <a-form-item label="物候期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['phenologicalPeriod']" :trigger-change="true" dictCode="phenological_period" placeholder="请选择物候期"/>
            </a-form-item>
          </a-col>
          <a-col :span="8" >
            <a-form-item label="果园负责人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['principal', validatorRules.principal]" placeholder="请输入果园负责人"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8" >
            <a-form-item label="负责人电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['phone', validatorRules.phone]" placeholder="请输入负责人电话"></a-input>
            </a-form-item>
          </a-col>
          <!-- <a-col :span="8" >
            <a-form-item label="果园介绍" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-editor v-decorator="['description',{trigger:'input'}]"/>
            </a-form-item>
          </a-col>
          <a-col :span="8" >
            <a-form-item label="果园图片" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload isMultiple v-decorator="['orchardPictures']"></j-image-upload>
            </a-form-item>
          </a-col>
          <a-col :span="8" >
            <a-form-item label="营销信息" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['marketingInformation']" placeholder="请输入营销信息"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8" >
            <a-form-item label="方针政策" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-editor v-decorator="['principlesPolicies',{trigger:'input'}]"/>
            </a-form-item>
          </a-col>
          <a-col :span="8" >
            <a-form-item label="果园规划" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-editor v-decorator="['orchardPlanning',{trigger:'input'}]"/>
            </a-form-item>
          </a-col> -->
        </a-row>
         <a-row :gutter='24'>
           <a-col :span="12" >
            <a-form-item label="果园介绍" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-editor v-decorator="['description',{trigger:'input'}]"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="果园图片" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload isMultiple v-decorator="['orchardPictures']"></j-image-upload>
            </a-form-item>
          </a-col>
           </a-row>
         <a-row :gutter='24'>
          <!-- <a-col :span="12" >
            <a-form-item label="营销信息" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['marketingInformation']" placeholder="请输入营销信息"></a-input>
            </a-form-item>
          </a-col> -->
          <a-col :span="12" >
            <a-form-item label="方针政策" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-editor v-decorator="['principlesPolicies',{trigger:'input'}]"/>
            </a-form-item>
          </a-col>
           <a-col :span="12" >
            <a-form-item label="果园规划" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-editor v-decorator="['orchardPlanning',{trigger:'input'}]"/>
            </a-form-item>
          </a-col>
           </a-row>
      </a-form>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="历年产量" :key="refKeys[0]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[0]"
          :loading="txppProductionRecordsTable.loading"
          :columns="txppProductionRecordsTable.columns"
          :dataSource="txppProductionRecordsTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
      <a-tab-pane tab="历年销售额" :key="refKeys[1]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[1]"
          :loading="txppSalesVolumeTable.loading"
          :columns="txppSalesVolumeTable.columns"
          :dataSource="txppSalesVolumeTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import pick from 'lodash.pick'
  import { getAction } from '@/api/manage'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDate from '@/components/jeecg/JDate'  
  import JImageUpload from '@/components/jeecg/JImageUpload'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JMultiSelectTag from "@/components/dict/JMultiSelectTag"
  import JEditor from '@/components/jeecg/JEditor'

  export default {
    name: 'TxppOrchardForm',
    mixins: [JEditableTableMixin],
    components: {
      JFormContainer,
      JDate,
      JImageUpload,
      JDictSelectTag,
      JMultiSelectTag,
      JEditor,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          name: {
            rules: [
              { required: true, message: '请输入果园名称!'},
            ]
          },
          address: {
            rules: [
              { required: true, message: '请输入果园地址!'},
            ]
          },
          createdTime: {
            rules: [
              { required: true, message: '请输入建园时间!'},
            ]
          },
          area: {
            rules: [
              { required: true, message: '请输入种植面积（亩）!'},
            ]
          },
          variety: {
            rules: [
              { required: true, message: '请输入果树品种!'},
            ]
          },
          principal: {
            rules: [
              { required: true, message: '请输入果园负责人!'},
            ]
          },
          phone: {
            rules: [
              { required: true, message: '请输入负责人电话!'},
            ]
          },
        },
        refKeys: ['txppProductionRecords', 'txppSalesVolume', ],
        tableKeys:['txppProductionRecords', 'txppSalesVolume', ],
        activeKey: 'txppProductionRecords',
        // 历年产量
        txppProductionRecordsTable: {
          loading: false,
          dataSource: [],
          columns: [
            // {
            //   title: '年份',
            //   key: 'particularYear',
            //   type: FormTypes.input,
            //   width:"200px",
            //   placeholder: '请输入${title}',
            //   defaultValue: '',
            // },
            {
              title: '产量',
              key: 'yieId',
              type: FormTypes.inputNumber,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '收录时间',
              key: 'collectionTime',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '操作人',
              key: 'operator',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        // 历年销售额
        txppSalesVolumeTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '年份',
              key: 'particularYear',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '销售额',
              key: 'salesVolume',
              type: FormTypes.inputNumber,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '收录时间',
              key: 'collectionTime',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '操作人',
              key: 'operator',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        url: {
          add: "/dome/txppOrchard/add",
          edit: "/dome/txppOrchard/edit",
          queryById: "/dome/txppOrchard/queryById",
          txppProductionRecords: {
            list: '/dome/txppOrchard/queryTxppProductionRecordsByMainId'
          },
          txppSalesVolume: {
            list: '/dome/txppOrchard/queryTxppSalesVolumeByMainId'
          },
        }
      }
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：false流程表单 true普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      addBefore(){
        this.form.resetFields()
        this.txppProductionRecordsTable.dataSource=[]
        this.txppSalesVolumeTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'info','name','address','createdTime','area','treeCount','variety','phenologicalPeriod','principal','phone','description','orchardPictures','marketingInformation','principlesPolicies','orchardPlanning')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.txppProductionRecords.list, params, this.txppProductionRecordsTable)
          this.requestSubTableData(this.url.txppSalesVolume.list, params, this.txppSalesVolumeTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          txppProductionRecordsList: allValues.tablesValue[0].values,
          txppSalesVolumeList: allValues.tablesValue[1].values,
        }
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          })
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'info','name','address','createdTime','area','treeCount','variety','phenologicalPeriod','principal','phone','description','orchardPictures','marketingInformation','principlesPolicies','orchardPlanning'))
     },

    }
  }
</script>

<style scoped>
</style>